home *** CD-ROM | disk | FTP | other *** search
- Path: news2.ios.com!usenet
- From: vlad@gramercy.ios.com (Vlastimil Adamovsky)
- Newsgroups: comp.lang.c++
- Subject: Re: Derived classes inter-referencing each other
- Date: Sat, 03 Feb 1996 22:32:52 GMT
- Organization: Internet Online Services
- Message-ID: <4f0n91$a3b@news2.ios.com>
- References: <4em3ta$2p0@delphi.cs.ucla.edu>
- NNTP-Posting-Host: ppp-47.ts-7.hck.idt.net
- X-Newsreader: Forte Free Agent 1.0.82
-
- geoff@ficus.cs.ucla.edu (Geoff Kuenning) wrote:
-
- >My problem hasn't gotten quite this bad, so I was able to solve it
- >by re-ordering declarations. But I started to wonder how one would
- >handle the more complex case.
-
- >Suppose you have a pair of derived classes, foo and bar, derived from
- >foobase and barbase respectively. The bases can be declared like
- >this:
-
- > class foobase {
- > virtual barbase& givebar();
- > };
- > class barbase {
- > virtual foobase& givefoo();
- > };
-
- >So far, so good. Now we declare the derived classes. However, we'd
- >like givebar() to return the corresponding derived class:
-
- > class foo : public foobase {
- > virtual bar& givebar();
- > };
-
- >Oops! Can't do that, because the compiler doesn't yet know that bar
- >is derived from barbase. I tried putting a null declaration before foo:
-
- > class bar : public barbase;
-
- >but unsurprisingly the compiler wasn't too happy with this. The only
- >solution I can come up with is to declare foo "incorrectly":
-
- > class foo : public foobase {
- > virtual barbase& givebar();
- > };
- > class bar : public barbase {
- > virtual foo& givefoo(); // This is asymmetric but correct
- > };
-
- >and then use typecasts to get the result of givebar() to be of type bar.
-
- >Anybody got a better solution?
- >--
- > Geoff Kuenning g.kuenning@ieee.org geoff@ITcorp.com
- > http://www.cs.ucla.edu/ficus-members/geoff/
-
- Unless there is some special reason behind your code, you could use a
- pointer instead a reference. Then, it shoud work with a forward
- declarations.
-
-
-
- *******************************************
- * Vlastimil Adamovsky *
- * Smalltalk, C++ and Envelop development *
- *******************************************
-
-